//
//  LocationCell.swift
//  MyLocations
//
//  Created by cygarm on 15/12/13.
//  Copyright © 2015年 cygarm. All rights reserved.
//

import UIKit

class LocationCell: UITableViewCell {
	@IBOutlet weak var descriptionLabel: UILabel!
	@IBOutlet weak var addressLabel: UILabel!
	@IBOutlet weak var photoImageView: UIImageView!
	
	override func awakeFromNib() {
		super.awakeFromNib()
		
		backgroundColor = UIColor.blackColor()
		descriptionLabel.textColor = UIColor.whiteColor()
		descriptionLabel.highlightedTextColor = descriptionLabel.textColor
		addressLabel.textColor = UIColor(white: 1.0, alpha: 0.4)
		addressLabel.highlightedTextColor = addressLabel.textColor
		
		let selectionView = UIView(frame: CGRect.zero)
		selectionView.backgroundColor = UIColor(white: 1.0, alpha: 0.2)
		selectedBackgroundView = selectionView
		
		photoImageView.layer.cornerRadius = photoImageView.bounds.size.width / 2
		photoImageView.clipsToBounds = true
		separatorInset = UIEdgeInsets(top: 0, left: 82, bottom: 0, right: 0)
		
		//for test
//		descriptionLabel.backgroundColor = UIColor.blueColor()
//		addressLabel.backgroundColor = UIColor.redColor()
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		if let sv = superview {
			descriptionLabel.frame.size.width = sv.frame.size.width - descriptionLabel.frame.origin.x - 10
			addressLabel.frame.size.width = sv.frame.size.width - addressLabel.frame.origin.x - 10
		}
	}
	
	func configureForLocation(location: Location) {
		if location.locationDescription.isEmpty {
			descriptionLabel.text = "(No Description)"
		} else {
			descriptionLabel.text = location.locationDescription
		}
		if let placemark = location.placemark {
			addressLabel.text = "\(placemark.subThoroughfare ?? "") \(placemark.thoroughfare ?? "")," + "\(placemark.locality ?? "")"
		} else {
			addressLabel.text = String(format: "Lat: %.8f, Long: %.8f", location.latitude, location.longitude)
		}
		photoImageView.image = imageForLocation(location)
	}
	
	func imageForLocation(location: Location) -> UIImage {
		if location.hasPhoto {
			if let image = location.photoImage {
				return image.resizedImageWithBounds(CGSize(width: 52, height: 52))
			}
		}
		return UIImage(named: "No Photo")!
	}
}
